package sk.tramtatiri.core;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


public class Parser {
	
	private List<Ingredient> ingredients = new ArrayList<Ingredient>(); 
	
	public Parser(String text) {
		text = text.toLowerCase().trim();

	    StringTokenizer stringTokenizer = new StringTokenizer(text, ",");

	    while (stringTokenizer.hasMoreTokens()) {
	        StringTokenizer stringToken = new StringTokenizer(stringTokenizer.nextToken().trim());

	        String name = "";
	        String weight = "";
	        Values value = Values.NOVALUE;
	        while (stringToken.hasMoreTokens()) {
	            String token = stringToken.nextToken().trim();
	            if (Character.isDigit(token.charAt(0))) {
	                char charact;
	                int i = 0;

	                while (Character.isDigit(charact = token.charAt(i)) | (charact + "").equals(".")) {
	                    weight += charact;
	                    i++;
	                    //comma f.e.: 2 , 5kg
	                    if (token.length() <= i) {
	                        if (stringToken.hasMoreTokens()) {
	                            token = stringToken.nextToken().trim();
	                            i = 0;
	                        } else if (stringTokenizer.hasMoreTokens()) {
	                            stringToken = new StringTokenizer(stringTokenizer.nextToken().trim());
	                            if (stringToken.hasMoreTokens()) {
	                                token = stringToken.nextToken().trim();
	                                weight += ".";
	                                i = 0;
	                            }
	                        }
	                    }
	                }
	                System.out.println("Weight=" + weight);
	                String val = token.substring(i).trim();
	                switch (value = Values.toValue(val)) {
	                    case GRAM:
	                        weight = (int) Math.round(Float.valueOf(weight)) + "";
	                        break;
	                    case DEKAGRAM:
	                        weight = (int) Math.round(100 * Float.valueOf(weight)) + "";
	                        break;
	                    case KILOGRAM:
	                        weight = (int) Math.round(1000 * Float.valueOf(weight)) + "";
	                        break;
	                    case LITER:
	                        weight = (int) Math.round(1000 * Float.valueOf(weight)) + "";
	                        break;
	                    case MILILITER:
	                        weight = (int) Math.round(Float.valueOf(weight)) + "";
	                        break;
	                    case DECILITER:
	                        weight = (int) Math.round(100 * Float.valueOf(weight)) + "";
	                        break;
	                    case NOVALUE:
	                        System.out.println("ERROR - Wrong value of weight Weight=" + weight + "; token=" + token + "; val=" + val + ";");
	                        break;
	                }
	            } else {
	                name += token + " ";
	            }
	        }
	        if(weight.isEmpty()){
	        	weight = "0";
	        }
	        ingredients.add(new Ingredient(name.trim(), Integer.valueOf(weight), value));
	    }
	}

	public List<Ingredient> getIngredients() {
		return ingredients;
	}
}